var _comments = [], _commentMap = {}, _currId = 0;

exports.list = function(onResults){
  onResults(_comments);
};

exports.get = function(id, onResult) {
    if (id) 
    {
    	console.log('api::get: ' + id);
    	result = _commentMap[id];
    	console.log('api::get::result: ' + JSON.stringify(result));
    }

	onResult(result);
}

exports.add = function(comment, onResult) {
    var result;
    
    if (comment)
    {
    	comment.id = ++_currId;
    	_comments.push(comment);
    	_commentMap[_currId] = comment;
        result = comment; 	
    }
	
	onResult(comment);
};

exports.delete = function(id, onResult) {
    var result = true;  // ignore if it doesn't exist
    if (id)
    {
        var idx = 0;
        for (; idx < _comments.length; ++idx)
        {
            if (_comments[idx].id == id)
            {
                _comments.splice(idx, 1);
                break;
            }
        }
        delete _commentMap[id];
    }

    onResult(result);
}
